import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { getAgentDashboard, getAgentDistributors, getAgentDistributorDetail, getAgentOrders, getAgentWithdrawals } from '@/api/agent'

// 区域代理状态管理
export const useAgentStore = defineStore('agent', () => {
  // 状态
  const agentInfo = ref({
    id: '',
    name: '',
    email: '',
    phone: '',
    avatar: '',
    region: '',
    commissionRate: 0,
    walletBalance: 0
  })

  const dashboard = ref({
    distributorCount: 0,
    activeDistributorCount: 0,
    salesOverview: {
      today: 0,
      week: 0,
      month: 0,
      total: 0
    },
    commissionOverview: {
      today: 0,
      week: 0,
      month: 0,
      total: 0,
      pending: 0,
      available: 0
    },
    pendingWithdrawals: 0,
    topDistributors: [],
    topProducts: []
  })

  const distributors = ref([])
  const distributorDetail = ref(null)
  const orders = ref([])
  const withdrawals = ref([])
  const loading = ref(false)
  const error = ref('')

  // Getters
  const totalSales = computed(() => dashboard.value.salesOverview.total)
  const totalCommission = computed(() => dashboard.value.commissionOverview.total)
  const availableCommission = computed(() => dashboard.value.commissionOverview.available)
  const distributorsList = computed(() => distributors.value)
  const ordersList = computed(() => orders.value)
  const withdrawalsList = computed(() => withdrawals.value)
  
  // 区域代理基本信息
  const setAgentInfo = (info) => {
    agentInfo.value = { ...info }
  }

  // 获取仪表盘数据
  const fetchDashboard = async () => {
    loading.value = true
    error.value = ''
    try {
      const response = await getAgentDashboard()
      dashboard.value = response.data
    } catch (err) {
      error.value = err.message || '获取仪表盘数据失败'
      console.error('获取仪表盘数据失败:', err)
    } finally {
      loading.value = false
    }
  }

  // 获取经销商列表
  const fetchDistributors = async (params = {}) => {
    loading.value = true
    error.value = ''
    try {
      const response = await getAgentDistributors(params)
      distributors.value = response.data.distributors
      return response.data
    } catch (err) {
      error.value = err.message || '获取经销商列表失败'
      console.error('获取经销商列表失败:', err)
      return null
    } finally {
      loading.value = false
    }
  }

  // 获取经销商详情
  const fetchDistributorDetail = async (id) => {
    loading.value = true
    error.value = ''
    try {
      const response = await getAgentDistributorDetail(id)
      distributorDetail.value = response.data
      return response.data
    } catch (err) {
      error.value = err.message || '获取经销商详情失败'
      console.error('获取经销商详情失败:', err)
      return null
    } finally {
      loading.value = false
    }
  }

  // 获取订单列表
  const fetchOrders = async (params = {}) => {
    loading.value = true
    error.value = ''
    try {
      const response = await getAgentOrders(params)
      orders.value = response.data.orders
      return response.data
    } catch (err) {
      error.value = err.message || '获取订单列表失败'
      console.error('获取订单列表失败:', err)
      return null
    } finally {
      loading.value = false
    }
  }

  // 获取提现列表
  const fetchWithdrawals = async (params = {}) => {
    loading.value = true
    error.value = ''
    try {
      const response = await getAgentWithdrawals(params)
      withdrawals.value = response.data.withdrawals
      return response.data
    } catch (err) {
      error.value = err.message || '获取提现列表失败'
      console.error('获取提现列表失败:', err)
      return null
    } finally {
      loading.value = false
    }
  }

  // 清除状态
  const resetState = () => {
    agentInfo.value = {
      id: '',
      name: '',
      email: '',
      phone: '',
      avatar: '',
      region: '',
      commissionRate: 0,
      walletBalance: 0
    }
    dashboard.value = {
      distributorCount: 0,
      activeDistributorCount: 0,
      salesOverview: {
        today: 0,
        week: 0,
        month: 0,
        total: 0
      },
      commissionOverview: {
        today: 0,
        week: 0,
        month: 0,
        total: 0,
        pending: 0,
        available: 0
      },
      pendingWithdrawals: 0,
      topDistributors: [],
      topProducts: []
    }
    distributors.value = []
    distributorDetail.value = null
    orders.value = []
    withdrawals.value = []
    error.value = ''
  }

  return {
    // 状态
    agentInfo,
    dashboard,
    distributors,
    distributorDetail,
    orders,
    withdrawals,
    loading,
    error,
    
    // Getters
    totalSales,
    totalCommission,
    availableCommission,
    distributorsList,
    ordersList,
    withdrawalsList,
    
    // Actions
    setAgentInfo,
    fetchDashboard,
    fetchDistributors,
    fetchDistributorDetail,
    fetchOrders,
    fetchWithdrawals,
    resetState
  }
}) 